package com.yixun.lbx.controller.company;

import com.yixun.controller.BaseController;
import com.yixun.entity.YxUser;
import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.entity.YxProCompanyProduct;
import com.yixun.lbx.service.CompanyProductService;
import com.yixun.lbx.service.CompanyService;
import com.yixun.lbx.service.CompanyStaffService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/*
 * 企业照片 控制器
 * */
@RestController
@RequestMapping("/cp")
public class CompanyProductController extends BaseController {
    private final CompanyProductService companyProductService;
    private final CompanyService companyService;
    private final CompanyStaffService companyStaffService;
    public CompanyProductController(CompanyProductService companyProductService, CompanyService companyService, CompanyStaffService companyStaffService) {
        this.companyProductService = companyProductService;
        this.companyService = companyService;
        this.companyStaffService = companyStaffService;
    }

    /**
     * @apiNote  addCompanyProduct 添加企业证照
     * @param map todo
     * @return int
     */
    @PostMapping("/addCompanyProduct")
    public int addCompanyProduct(@RequestBody HashMap<String, Object> map) {
        String user_name = this.getSessionUser().getUser_name();
        YxProCompany company = new YxProCompany();
        company.setCode(user_name);
        Long qyId = companyService.getCompanyByIdOrNameOrCode(company).getQyId();
        map.put("qyId", qyId);
        map.put("addUser", this.getSessionUser().getUser_id());
        return companyProductService.addCompanyProduct(map);
    }
    /**
     * @apiNote  updateCompanyProduct 更新企业证照
     * @param map todo
     * @return int
     */
    @PostMapping("/updateCompanyProduct")
    public int updateCompanyProduct(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        YxProCompanyProduct one = companyProductService.getOneById((String) map.get("id"));
        companyStaffService.deleteFile(one.getImgUrl());
        return companyProductService.updateCompanyProduct(map);
    }
    /**
     * @apiNote  deleteCompanyProduct 删除企业证照
     * @param map todo
     * @return int
     */
    @PostMapping("/deleteCompanyProduct")
    public int deleteCompanyProduct(@RequestBody HashMap<String, Object> map) {
        String[] ids = map.get("id").toString().split(",");
        for (String id : ids) {
            YxProCompanyProduct one = companyProductService.getOneById(id);
        }
        map.put("ids", ids);
        map.put("updateUser", this.getSessionUser().getUser_id());
        return companyProductService.deleteCompanyProduct(map);
    }

    /**
     * @apiNote  getCompanyProductByArgs 通过字段 查询CompanyProduct 该字段可以为任何一个或者多个字段
     * @param companyProduct todo
     * @return java.util.List<com.yixun.lbx.entity.YxProCompanyProduct>
     */
    @GetMapping("/getCompanyProductByArgs")
    public List<YxProCompanyProduct> getCompanyProductByArgs(YxProCompanyProduct companyProduct) {
        return companyProductService.getCompanyProductByArgs(companyProduct);
    }

    /**
     * @apiNote  getListByPage 企业照片（分页）列表
     * @param map todo
     * @return java.util.HashMap<java.lang.String,java.lang.Object>
     */
    @RequestMapping("/getListByPage")
    public HashMap<String, Object> getListByPage(@RequestBody HashMap<String, String> map) {
        HashMap<String, Object> hashMap = new HashMap<>();
        Integer companyCount;
        List<YxProCompanyProduct> companies;
        YxUser sessionUser = this.getSessionUser();
        String qYCode = sessionUser.getUser_name();
        map.put("code", qYCode);
        companyCount = companyProductService.CpCount(map);
        companies = companyProductService.getListByPage(map);
        hashMap.put("companyCount", companyCount);
        hashMap.put("companies", companies);
        return hashMap;
    }

    /**
     * @apiNote  getSearchListByPage 搜索
     * @param map todo
     * @return java.util.HashMap<java.lang.String,java.lang.Object>
     */
    @RequestMapping("/getSearchListByPage")
    public HashMap<String, Object> getSearchListByPage(@RequestBody HashMap<String, String> map) {
        HashMap<String, Object> hashMap = new HashMap<>();
        map.put("qyId", String.valueOf(getQyId()));
        List<YxProCompanyProduct> companies = companyProductService.getSearchListByPage(map);
        hashMap.put("companies", companies);
        hashMap.put("count", companies.size());
        return hashMap;
    }
    private Long getQyId() {
        YxProCompany yxProCompany = new YxProCompany();
        yxProCompany.setCode(this.getSessionUser().getUser_name());
        YxProCompany company = companyService.getCompanyByIdOrNameOrCode(yxProCompany);
        if (company != null) {
            return company.getQyId();
        } else {
            return 0L;
        }
    }
}
